package com.helloUIBinder.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import com.helloUIBinder.client.events.OnClosePopupEvent;
import com.helloUIBinder.client.events.OnClosePopupHandler;
import com.helloUIBinder.client.receivers.SecurityContextHolderReceiver;

public class AdminHeaderWidget extends Composite {
    private static AdminHeaderWidgetUiBinder uiBinder = GWT.create(AdminHeaderWidgetUiBinder.class);

    interface AdminHeaderWidgetUiBinder extends UiBinder<Widget, AdminHeaderWidget> {
    }

    @UiField
    Label usernameLabel;

    @UiField
    Anchor loginLogoutLink;

    @UiField
    Anchor homeLink;

    @UiField
    Anchor homeLinkImage;

    @UiField
    Anchor aboutLink;

    @UiField
    Anchor workLink;

    @UiField
    Anchor serviceLink;

    @UiField
    Anchor blogLink;

    @UiField
    Anchor contactLink;

    @UiField
    Anchor adminLink;

    protected SimpleEventBus eventBus = new SimpleEventBus();
    private AdminPopupWidget adminPopupWidget;

    public AdminHeaderWidget() {
        initWidget(uiBinder.createAndBindUi(this));
        SecurityContextHolderReceiver.getCurrentUserLogin(getLoginCallBack());
        loginLogoutLink.setHref("/index.html");
        loginLogoutLink.setText("Login");

        eventBus.addHandler(OnClosePopupEvent.TYPE, new OnClosePopupHandler() {
            @Override
            public void close(OnClosePopupEvent event) {
                RootPanel.get().remove(adminPopupWidget);
            }
        });
    }


    private RequestCallback getLoginCallBack() {
        return new RequestCallback() {
            @Override
            public void onResponseReceived(Request request, Response response) {
                String currentUserLogin = response.getText();
                usernameLabel.setText(currentUserLogin + " is currently logged in!");
                if (!currentUserLogin.equals("anonymousUser")) {
                    loginLogoutLink.setHref("/j_spring_security_logout");
                    loginLogoutLink.setText("Logout");
                }
            }

            @Override
            public void onError(Request request, Throwable exception) {
                Window.alert("Error occurred" + exception.getMessage());
            }
        };
    }

    private void changeBodyTo(Widget widget) {
        BodyContainerWidget currentBodyContainerWidget = (BodyContainerWidget) RootPanel.get().getWidget(1);
        currentBodyContainerWidget.getBodyContainer().remove(0);
        currentBodyContainerWidget.getBodyContainer().add(widget);
    }

    @UiHandler("homeLink")
    void onHomeWidgetClick(ClickEvent event) {
        HomePageWidget homePageWidget = new HomePageWidget();

        changeBodyTo(homePageWidget);
    }

    @UiHandler("homeLinkImage")
    void onHomeWidgetImageClick(ClickEvent event) {
        HomePageWidget homePageWidget = new HomePageWidget();

        changeBodyTo(homePageWidget);
    }

    @UiHandler("aboutLink")
    void onAboutWidgetClick(ClickEvent event) {
        AboutPageWidget aboutPageWidget = new AboutPageWidget();

        changeBodyTo(aboutPageWidget);
    }

    @UiHandler("workLink")
    void onWorkWidgetClick(ClickEvent event) {
        WorkWidget workWidget = new WorkWidget();

        changeBodyTo(workWidget);
    }

    @UiHandler("serviceLink")
    void onServiceWidgetClick(ClickEvent event) {
        ServicePageWidget servicePageWidget = new ServicePageWidget();

        changeBodyTo(servicePageWidget);
    }

    @UiHandler("blogLink")
    void onBlogWidgetClick(ClickEvent event) {
        BlogPageWidget blogPageWidget = new BlogPageWidget();

        changeBodyTo(blogPageWidget);
    }

    @UiHandler("contactLink")
    void onContactWidgetClick(ClickEvent event) {
        ContactPageWidget contactPageWidget = new ContactPageWidget();

        changeBodyTo(contactPageWidget);
    }

    @UiHandler("adminLink")
    void onAdminLinkClick(ClickEvent event) {
//        adminPopupWidget = new AdminPopupWidget(eventBus);
//        RootPanel.get().add(adminPopupWidget);
        AdminPageWidget adminPageWidget = new AdminPageWidget();
        changeBodyTo(adminPageWidget);
    }
}
